package com.goodboys.utils;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by CJ on 2018-03-29.
 */
public class JsoupUtils {

    private final static Logger logger = LoggerFactory.getLogger(JsoupUtils.class);


    private final  static String client_id  = "5ae7f36a77d8a07791693247";

    private final  static String client_secret  = "926b83ee923a9514f8b241b6e68a30fc";

    //获取token值application/x-www-form-urlencoded
    public static  String getToken(String code) throws IOException {
        try {
            Connection connection = Jsoup.connect("https://oauth.onlyid.net/token")
                    .header("Accept", "text/html, application/xhtml+xml, */*")
                    .header("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0))")
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .data("client_id", client_id)
                    .data("client_secret", client_secret)
                    .data("grant_type", "authorization_code")
                    .data("code", code)
                    .data("redirect_uri", "http://lyou.usts.top/lyou/sys/dynamicLogin")
                    .method(Connection.Method.POST)
                    .timeout(8000)
                    .ignoreContentType(true);
//            Document post = connection.post();
//            String body  = post.toString();
            Connection.Response execute = connection.execute();
            String body = execute.body();
            return  body;
        } catch (IOException e) {
            Connection connection = Jsoup.connect("https://oauth.onlyid.net/token")
                    .header("Accept", "text/html, application/xhtml+xml, */*")
                    .header("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0))")
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .data("client_id", client_id)
                    .data("client_secret", client_secret)
                    .data("grant_type", "authorization_code")
                    .data("code", code)
                    .data("redirect_uri", "http://lyou.usts.top/lyou/sys/dynamicLogin")
                    .method(Connection.Method.POST)
                    .timeout(8000)
                    .ignoreContentType(true);
//            Document post = connection.post();
//            String body  = post.toString();
            Connection.Response execute = connection.execute();
            String body = execute.body();
            return  body;
        }
    }

    public static void main(String[] args) throws IOException {
        String e7ce57462bb92ade7e382f3c586149d648aee4e9 = getToken("e7ce57462bb92ade7e382f3c586149d648aee4e9");
        System.out.println(e7ce57462bb92ade7e382f3c586149d648aee4e9);
    }

    //获取源码
    public static String getHtml(String url)  throws  IOException{
        try {
            return Jsoup.connect(url)
                    .timeout(8000)
                    .ignoreContentType(true)
                    .execute()
                    .body();
        } catch (IOException e) {
            return Jsoup.connect(url)
                    .timeout(8000)
                    .ignoreContentType(true)
                    .execute()
                    .body();

        }
    };

    //获取document对象
    public static Document getDocument(String url) throws IOException {
        try {
            return Jsoup.connect(url)
                    .ignoreContentType(true)
                    .timeout(8000)
                    .get();
        } catch (IOException e) {
            return Jsoup.connect(url)
                    .ignoreContentType(true)
                    .timeout(8000)
                    .get();
        }
    }

    //设置请求头信息
    //获取源码
    public static Connection getConnection(String url) throws IOException {
        return Jsoup.connect(url)
                .timeout(8000)
                .ignoreContentType(true);
    };



}
